package com.bstek.urule.springboot;

import com.bstek.urule.ClassUtils;
import com.bstek.urule.Utils;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.service.KnowledgeService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class UruleTest {

    @Test
    public void test() throws Exception{
        //从Spring中获取KnowledgeService接口实例
        KnowledgeService service=(KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        //通过KnowledgeService接口获取指定的资源包"test123"
        KnowledgePackage knowledgePackage=service.getKnowledge("projectName/test123");
        //通过取到的KnowledgePackage对象创建KnowledgeSession对象
        KnowledgeSession session= KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);

        Employee employee=new Employee();
        Dept dept=new Dept();
        dept.setLevel(12);
        employee.setDept(dept);
        employee.setSalary(111000);
        //将业务数据对象Employee插入到KnowledgeSession中
        session.insert(employee);
        //执行所有满足条件的规则

        Map<String,Object> parameter=new HashMap<String,Object>();
        parameter.put("count", 10);
        parameter.put("result", true);
        //触发规则时并设置参数
        session.fireRules(parameter);

        //获取计算后的result值，要通过KnowledgeSession,而不能通过原来的parameter对象
        boolean result=(Boolean)session.getParameter("result");
        System.out.println(result);
    }

    public void test1(){
        File file=new File("d:/customer.xml");
        ClassUtils.classToXml(Customer.class, file);
    }

}
